package com.tpc.server.systeme.service;

import java.sql.ResultSet;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.tpc.server.systeme.utils.DatabaseManager;
import com.tpc.server.systeme.utils.SecuriteManager;
import java.util.logging.Logger;
import com.tpc.client.systeme.core.service.UserService;
import com.tpc.dao.extend.User;
import com.tpc.dao.extend.Ressource;

/**
 *
 * @author frederic.fortin
 */

@SuppressWarnings("serial")
public class UserServiceImpl extends RemoteServiceServlet implements UserService {

    private static final long serialVersionUID = 3L;
    private final static Logger objLog = Logger.getLogger(UserServiceImpl.class.getName());

    public UserServiceImpl() {
        
    }

    public User[] loadList() {

        return null;
    }

    public Ressource[] loadListRessource() {
        try {
            Ressource[] arrobjRessource = null;
            SecuriteManager objSecurite = new SecuriteManager(this.getThreadLocalRequest());
            if (objSecurite.isAccessAllowed(User.class.getSimpleName(),"READ")) {
                DatabaseManager objBD = objSecurite.getDataBaseManager();
                /** @TODO : should get parameter (life = ENABLE) from properties inside session  **/
                String strQueryLoadListRessource = "SELECT USR.id AS ID, firstname, lastname FROM user USR, user_ressource,user_has_groupe "
                        + "WHERE USR.life = ? AND USR.id = user_has_groupe.userId AND user_has_groupe.groupId = ? AND USR.id = user_ressource.userId ORDER BY firstname";
                String[] arrstrParameters = {objSecurite.getProperties().getProperty("ENABLE"),objSecurite.getProperties().getProperty("GROUP_SALES")};
                ResultSet objRS = objBD.executeQuery(strQueryLoadListRessource, arrstrParameters);
                if (objRS != null) {
                    int i = 0;
                    int intNbrRow = 0;
                    objRS.last();
                    intNbrRow = objRS.getRow();
                    arrobjRessource = new Ressource[intNbrRow];
                    objRS.beforeFirst();
                    while (objRS.next()) {
                
                        arrobjRessource[i] = new Ressource();
                        arrobjRessource[i].setId(objRS.getInt("ID"));
                        arrobjRessource[i].setFirstName(objRS.getString("firstname"));
                        arrobjRessource[i].setLastName(objRS.getString("lastname"));

                        i++;
                    }
                }
                objBD.closeConnection();
            }
            return arrobjRessource;
        }
        catch (Exception objx) {
            objLog.info("message 2: "+objx);
            return null;
        }
    }

    public User load(String strId) {

        return null;
    }

    public int create(User objUsr) {
        try {
            User[] arrobjUser = null;
            if (new SecuriteManager(this.getThreadLocalRequest()).isAccessAllowed(User.class.getSimpleName(),"EXEC")) {
                //arrobjOrganisation =
            }
            return 1;
        }
        catch (Exception objx) {
            objLog.info("message 2: "+objx);
            return -1;
        }
    }

    /** Update/Modify **/
    public int modify(String[] arrstrId) {
        try {
            User[] arrobjUser = null;
            if (new SecuriteManager(this.getThreadLocalRequest()).isAccessAllowed(User.class.getSimpleName(),"EXEC")) {
                //arrobjOrganisation =
            }
            return 1;
        }
        catch (Exception objx) {
            objLog.info("message 2: "+objx);
            return -1;
        }
    }

    /** Remove/Delete **/
    public int delete(String[] arrstrId) {
        try {
            User[] arrobjUser = null;
            if (new SecuriteManager(this.getThreadLocalRequest()).isAccessAllowed(User.class.getSimpleName(),"EXEC")) {
                //arrobjOrganisation =
            }
            return 1;
        }
        catch (Exception objx) {
            objLog.info("message 2: "+objx);
            return -1;
        }
    }

}
